<?php

require_once ('common.php');
checkLogin();
$action = $_POST['action'];
$id = trim($_REQUEST['id']);
if (empty($id))
    $id = $uid;
$owner = ($id == $uid) ? 1 : 0;
$users = getUserProfile($id);
if (!$users)
{
    header('location: ' . APP_URL);
    exit;
}
if (!$owner)
{
    $is_buddy = in_array($id, $user['buddylist']) ? 1 : 0;
}
else
{
    $pageid = 'profile';
    $backgrounds = getBackground();
}
if ($action == 'editprofile')
{
    $member_realname = trim(stripslashes(htmlspecialchars($_POST['realname'])));
    $member_email = trim(stripslashes(htmlspecialchars($_POST['email'])));
    $member_sex = $_POST['sex'];
    editProfile($id, $member_realname, $member_sex, $member_email);
    header('location: ' . APP_URL . '/profile');
    exit;
}
elseif ($action == 'editphoto')
{
    $member_avatar = $_POST['avatar'];
    if (!empty($_FILES['image']['tmp_name']))
    {
        $imageFile = $_FILES['image']['name'];
        $imageTmp = $_FILES['image']['tmp_name'];
        $imageType = str_replace('image/', '', $_FILES['image']['type']);
        do
        {
            $imageName = genChar() . '.' . $imageType;
            $imagePath = AVATAR_PATH . '/' . $imageName;
        }
        while (file_exists($imagePath));
        $member_avatar = $imageName;
        if (!in_array($imageType, array('gif', 'jpg', 'jpeg', 'png')))
            $error = 'ประเภทไฟล์ไม่ถูกต้อง เฉพาะไฟล์รูปภาพเท่านั้น';
        if (empty($error))
        {
            if (file_exists($imagePath))
                unlink($imagePath);
            cropImage(64, 64, $imageTmp, $imageType, $imagePath);
        }
    }
    if (empty($error))
    {
        editPhoto($id, $member_avatar);
        header('location: ' . APP_URL . '/profile');
        exit;
    }	
}
elseif ($action == 'editpassword')
{
    $oldpassword = trim(htmlspecialchars($_POST['oldpassword']));
    $newpassword = trim(htmlspecialchars($_POST['newpassword']));
    if (!empty($oldpassword) && !empty($newpassword))
    {
        if (sha1(md5($oldpassword)) != $users['password'])
        {
            $error = 'รหัสผ่านเดิมไม่ถูกต้อง กรุณาลองใหม่อีกครั้ง';
        }
        else
        {
            $newpassword = sha1(md5($newpassword));
            if (editPassword($uid, $newpassword))
            {
                $success = 'เปลี่ยนรหัสผ่านเรียบร้อยแล้ว';
            }
        }
    }
}
elseif ($action == 'togglebuddy')
{
	checkSession($_POST['session']);
	$check = $db->getRow('SELECT friendid FROM bbs_buddy WHERE friendid = %1 AND uid = %2 LIMIT 1', $id, $uid);
    if (!empty($check))
    {
        $db->query('DELETE FROM bbs_buddy WHERE ((friendid = %1 AND uid = %2) OR (uid = %1 AND friendid = %2)) LIMIT 2', $id, $uid);
        die(json_encode(array('status' => 'success', 'type' => 'unbuddy', 'message' => 'เลิกเป็นเพื่อนเรียบร้อยแล้ว')));
    }
    else
    {
        $db->insertRow('bbs_buddy', array('uid' => $uid, 'friendid' => $id, 'lastactivity' => getTime()));
        $db->insertRow('bbs_buddy', array('uid' => $id, 'friendid' => $uid, 'lastactivity' => getTime()));
        die(json_encode(array('status' => 'success', 'type' => 'isbuddy', 'message' => 'เพิ่มเป็นเพื่อนเรียบร้อยแล้ว')));
    }
}
elseif ($action == 'editdesign')
{
	$fontcolor = trim(htmlspecialchars($_POST['fontcolor']));
	$linkcolor = trim(htmlspecialchars($_POST['linkcolor']));
	$bgcolor = trim(htmlspecialchars($_POST['bgcolor']));
	$bgimage = trim(htmlspecialchars($_POST['bgimage']));
	$cols_data = array(
		'uid' => $id,
		'fontcolor' => $fontcolor,
		'linkcolor' => $linkcolor,
		'bgcolor' => $bgcolor,
		'bgimage' => $bgimage
	);
	$check = $db->getRow('SELECT uid FROM bbs_themes WHERE uid = %1', $id);
	if (!empty($check)) 
		$db->updateRow('bbs_themes', array('uid' => $id), $cols_data);
	else
		$db->insertRow('bbs_themes', $cols_data);
	header('location: '.APP_URL.'/profile');
}
$pagetitle = 'ข้อมูลส่วนตัวของ ' . $users['realname'];
$smarty->assign('pageid', $pageid);
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('owner', $owner);
$smarty->assign('users', $users);
$smarty->assign('error', $error);
$smarty->assign('success', $success);
$smarty->assign('is_buddy', $is_buddy);
$smarty->assign('buddy_list', $buddy_list);
$smarty->assign('backgrounds', $backgrounds);
$smarty->display('profile.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>